/*
 *
 * File Created at 2018/9/6
 *
 * Copyright 2016 CMCC Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ZYHY Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license.
 */
package com.chang.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.chang.common.resultcode.StatusCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;


@NoArgsConstructor
public class Result<K, V> implements Serializable {

    private static final long serialVersionUID = -6013178372858137407L;

    private int recode;
    private String msg;
    private V data;

    /**
     * @param recode
     * @param msg
     */
    public Result(int recode, String msg) {
        super();
        this.recode = recode;
        this.msg = msg;
    }

    /**
     * @param value
     * @return Result<String, Object>
     * @despciption
     */
    public static Result<String, Object> create(Object value) {
        Result<String, Object> result = new Result<String, Object>();
        result.setData(JSONObject.toJSON(value));
        result.setMsg(StatusCode.SUCCESS.getMsg());
        result.setRecode(StatusCode.SUCCESS.getCode());
        return result;
    }

    /**
     * @param value
     * @return
     */
    public static Result<String, Object> create2(Object value) {
        Result<String, Object> result = new Result<String, Object>();
        result.setData(value);
        result.setMsg(StatusCode.SUCCESS.getMsg());
        result.setRecode(StatusCode.SUCCESS.getCode());
        return result;
    }

    /**
     * @param value
     * @return Result<String, Object>
     * @despciption
     */
    public static Result<String, Object> create(Object value, StatusCode statusCode) {
        Result<String, Object> result = new Result<String, Object>();
        result.setData(JSONObject.toJSON(value));
        result.setMsg(statusCode.getMsg());
        result.setRecode(statusCode.getCode());
        return result;
    }

    /**
     * @param message
     * @return Result<String, Object>
     * @despciption
     */
    public static Result<String, Object> error(StatusCode statusCode, String message) {
        Result<String, Object> result = new Result<String, Object>();
        if (null == message || 0 == message.length()) {
            result.setMsg(statusCode.getMsg());
        } else {
            result.setMsg(message);
        }
        result.setRecode(statusCode.getCode());
        return result;
    }

    /**
     * @param statusCode
     * @param message
     * @param value
     * @return
     */
    public static Result<String, Object> error(StatusCode statusCode, String message, Object value) {
        Result<String, Object> result = new Result<String, Object>();
        if (null == message || 0 == message.length()) {
            result.setMsg(statusCode.getMsg());
        } else {
            result.setMsg(message);
        }
        result.setRecode(statusCode.getCode());
        result.setData(JSONObject.toJSON(value));
        return result;
    }

    //    public boolean isSucc() {
    //        return StatusCode.SUCCESS.getCode() == this.recode;
    //    }

    public int getRecode() {
        return recode;
    }

    public void setRecode(int recode) {
        this.recode = recode;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public V getData() {
        return data;
    }

    public void setData(V data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return String.format("Recode = %s, Msg = %s", String.valueOf(this.recode), this.msg);
    }

}

/**
 * Revision history
 * -------------------------------------------------------------------------
 * <p>
 * Date Author Note
 * -------------------------------------------------------------------------
 * 2019/06/18 zhouleibin create
 */
